#!/usr/bin/env python

# copyright 2010 Studio Cypher, LLC
# developer: Will Emigh
# projects@studiocypher.com

# Custom modules
from authentication import user_required
from authentication import get_account_from_user
from models import Account
from models import Colony
from models import Planet
from models import Log
from models import BuildingType
from models import BuildingUnit

# Built-in or Google modules
import cgi
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from google.appengine.ext import db

        
class CollectResources(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()

        # If they don't already have an account, redirect to main
        account = get_account_from_user(user)
        if (account == None):
            self.redirect("/")
            return
            
        colony = None
        for colony_key in account.current_planet.colonies:
            colony = Colony.get(colony_key)
            if colony.owner.key() == account.key():
                colonized = True
                break
        colony_key = self.request.get("colony_key")
        colony = db.get(colony_key)
        planet = account.current_planet

        allbuildings = BuildingUnit.all()
        userbuildings = allbuildings.filter('owner =',account).filter('planet =', planet)
        increase = 0
        for building in userbuildings:
            if (building.type.name == 'Supercooled Magnetic Extractor Upgrade'):
                increase = 5
            elif (building.type.name == 'Deep Core Mining Extractor Upgrade'):
                increase = 10
                break
        pollution = int(planet.pollution)
        if ((pollution - increase) <= 0):
            pollution = 0
        else:
            pollution -= increase
            

        planet = account.current_planet
        #num_colonists = len(planet.colonies)
        num_colonists = 1 #so that only pollution affects extraction

        amt_to_share = planet.resourceStorage.resourceOne
        share = amt_to_share/num_colonists
        #added so that extraction depends on pollution
        share = share - pollution/5
        if (share<=0): share=0
        colony.resourceStorage.resourceOne += share

        amt_to_share = planet.resourceStorage.resourceTwo
        share = amt_to_share/num_colonists
        #added so that extraction depends on pollution
        share = share - pollution/5
        if (share<=0): share=0
        #share = planet.waterAdjustForPollution(share) * increase
        colony.resourceStorage.resourceTwo += share

        amt_to_share = planet.resourceStorage.resourceThree
        share = amt_to_share/num_colonists
        #added so that extraction depends on pollution
        share = share - pollution/5
        if (share<=0): share=0
        colony.resourceStorage.resourceThree += share

        amt_to_share = planet.resourceStorage.resourceFour
        share = amt_to_share/num_colonists
        #added so that extraction depends on pollution
        share = share - pollution/5
        if (share<=0): share=0
        #share = planet.organicAdjustForPollution(share) * increase
        colony.resourceStorage.resourceFour += share

        colony.collectable = True #get rid of the timer
        colony.resourceStorage.put()
        colony.put()

        #log action
        new_log = Log.log(account.email, "Collected ", planet.name,
                          "Resources after collecting: Minerals: " + str(colony.resourceStorage.resourceOne) +
                          "Water: " + str(colony.resourceStorage.resourceTwo) +
                          "Fuels: " + str(colony.resourceStorage.resourceThree) +
                          "Organic: " + str(colony.resourceStorage.resourceFour))
        #end of log action
        
        #add pollution to the planet
        planet.pollution += 1
        planet.put()
        account.put()

        self.redirect('/planet/view/?id=' + str(account.current_planet.key()))    


        
application = webapp.WSGIApplication(
                                     [('/collect/resources/', CollectResources)],
                                     debug=True)									 
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
